package com.ns.dcjh.ui.activity;


import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.ns.dcjh.MyApplication;
import com.ns.dcjh.R;
import com.ns.dcjh.base.BaseActivity;
import com.ns.dcjh.constant.Constant;
import com.ns.dcjh.utils.IntentUtils;
import com.ns.dcjh.utils.SharedPreferencesUtils;
import com.ns.dcjh.utils.StatusBarUtils;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiguang.api.utils.JCollectionAuth;

@SuppressLint("NonConstantResourceId")
public class UserPrivacyPolicyDialogActivity extends BaseActivity {
    @BindView(R.id.txt_tip)
    TextView txtTip;

    String regId = "";

    protected void initStatusBar() {
        StatusBarUtils.setTranslucentStatusAndDark(mContext);
    }

    @Override
    protected void initTransition() {
        overridePendingTransition(R.anim.down_in, R.anim.at_rest);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.at_rest, R.anim.up_out);
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_privacy_policy_dialog;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
        String text = "感谢您信任并选择全房源系统App，我们非常重视您的个人信息与隐私！依据最新的监管要求，我们特向您说明如下：<br/>" +
                "1、为向您提供房源管理等服务，我们将依据法律及<a href='" + Constant.URL_PROTOCOL + "'>《用户协议》</a>和"+
                "<a href='" + Constant.URL_PRIVACY_POLICY + "'>《隐私声明》</a>收集、使用提供服务所必须的个人信息；<br />" +
                "2、基于您的使用授权，我们可能会调取您的相机、存储空间等设备权限，您有权拒绝或取消授权；<br />" +
                "3、我们会采取严格的技术措施和管理措施以保护您的信息安全；<br />" +
                "4、未经您同意，我们不会向第三方共享您的个人信息；<br />";

        interceptHyperLink(txtTip, text);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void dataRequest() {

    }

    /**
     * 格式化超链接文本内容并设置点击处理
     * */
    private CharSequence getClickableHtml(String html) {
        Spanned spannedHtml = Html.fromHtml(html);
        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);

        for(final URLSpan span : urls) {
            setLinkClickable(clickableHtmlBuilder, span);
        }
        return clickableHtmlBuilder;
    }

    /**
     * 设置点击超链接对应的处理内容
     * */
    private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder,final URLSpan urlSpan) {
        int start = clickableHtmlBuilder.getSpanStart(urlSpan);
        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);

        ClickableSpan clickableSpan = new ClickableSpan() {
            public void onClick(View view) {
                Logger.d(urlSpan.getURL());
                //LogW.i("URL-click:"+urlSpan.getURL());
            }
        };

        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
    }

    /**
     * 拦截超链接
     * @param tv
     */
    private void interceptHyperLink(TextView tv,String content) {
        CharSequence text = Html.fromHtml(content);
        if (text instanceof Spannable) {
            int end = text.length();
            Spannable spannable = (Spannable) text;
            URLSpan[] urlSpans = spannable.getSpans(0, end, URLSpan.class);

            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
            for (URLSpan uri : urlSpans) {
                String url = uri.getURL();
                if (url.indexOf("http://") == 0 || url.indexOf("https://")==0) {
                    CustomUrlSpan customUrlSpan = new CustomUrlSpan(mContext, url);
                    spannableStringBuilder.setSpan(customUrlSpan, spannableStringBuilder.getSpanStart(uri),
                            spannableStringBuilder.getSpanEnd(uri), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    spannableStringBuilder.removeSpan(uri);
                }
            }
            tv.setText(spannableStringBuilder);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }

    }

    private class CustomUrlSpan extends ClickableSpan {
        private Context context;
        private String url;

        public CustomUrlSpan(Context context, String url){
            this.context = context;
            this.url = url;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(ContextCompat.getColor(mContext, R.color.color_primary));
        }

        @Override
        public void onClick(View widget) {
            IntentUtils.showIntent(
                    mContext,
                    WebviewActivity.class,
                    new String[]{"url"},
                    new String[]{url}
            );
        }
    }

    @OnClick(R.id.btn_reject)
    public void btnRejectOnClick() {
        JCollectionAuth.setAuth(getApplicationContext(),false);
        System.exit(0);
    }

    @OnClick(R.id.btn_agree)
    public void btnAgreeOnClick() {
        SharedPreferencesUtils sp = SharedPreferencesUtils.getInstance(mContext);
        sp.setObject(Constant.SP_KEY_USER_AGREE_PRIVACY_POLICY, "YES");
        IntentUtils.showIntent(mContext, WebviewX5Activity.class);
        ((MyApplication) getApplication()).initThirdSdk();
        finish();
    }
}